#include<stdio.h>
#define max 10000000l
#define num 50000
int i,j,k,m,n,h[num],visited[num];
int c[num][2],pos[num],pre[num];
long d[num];
int nHeap,heap[num];
void init(){
    for(i=1;i<=n;i++){
        d[i]=max;
        visited[i]=0;
        pos[i]=pre[i]=0;
    }
    d[1]=0;
}       
void input(){
    int k[16000][3];
    FILE *fp=stdin;
    fscanf(fp,"%d%d",&n,&m);
    init();
    for(i=1;i<=m;i++){
        fscanf(fp,"%d%d%d",&k[i][0],&k[i][1],&k[i][2]);
        h[k[i][0]]++;
        h[k[i][1]]++;
    }
    for(i=2;i<=n;i++)h[i]+=h[i-1];
    for(i=1;i<=m;i++){
        c[h[k[i][0]]][0]=k[i][1];
        c[h[k[i][0]]][1]=k[i][2];
        
        
        c[h[k[i][1]]][0]=k[i][0];
        c[h[k[i][1]]][1]=k[i][2];
        
        h[k[i][0]]--;h[k[i][1]]--;
    }
    h[n+1]=m+1;
}
/******************* Heap **********************/
void update(long v){
    long child=pos[v],parent;
    if(child==0)child=++nHeap;
    parent=child/2;
    while(parent>0 && d[heap[parent]]>d[v]){
        heap[child]=heap[parent];
        pos[heap[child]]=child;
        child=parent;
        parent=child/2;
    }
    heap[child]=v;
    pos[v]=child;
}
long pop(){
    long r=1,c,v=heap[nHeap],ret=heap[1];
    nHeap--;
    while(r*2<nHeap){
        c=r*2;
        if(c<nHeap && d[heap[c+1]]<d[heap[c]]) c++;
        if(d[v]<=d[heap[c]])break;
        pos[heap[r]]=r;
        r=c;
    }
    heap[r]=v;
    pos[v]=r;
    return ret;
}
void prim(){
    int m=1,u,canh;
    update(1);
    do{
        u=pop();
        visited[u]=1;
        for(i=h[u]+1;i<=h[u+1];i++){
            canh=c[i][0];
            if(d[canh]>c[i][1]){
                d[canh]=c[i][1];
                pre[canh]=u;
                update(canh);
            }
        }
    }while(nHeap>0);
}
void output(){
    long a=0;
    for(i=1;i<=n;i++)a+=d[i];
    printf("%ld",a);
}
int main(){
    input();
    prim();
    output();
    return 0;
}
        
